﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Collections;
using System.IO;
namespace TKMT_Nhom13
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        //Kha Nguyen - 5/18/2013 - begin
        //Bien toan cuc
        private string fileCSV;		//Ten day du cua tap tin voi duong dan
        private string dirCSV;		//Duong dan cua tap tin
        private string fileNevCSV; //ten tap tin ( voi phan moi rong )
        private int numberOfRows = 50000; // mac dinh so luong dong se doc tu tap tin
        private int iSoCot;
        private int C1;
        private int C2;
        private float PC1;
        private float PC2;
        ArrayList Age=new ArrayList();
        ArrayList Sex= new ArrayList();
        ArrayList Region1=new ArrayList();
        ArrayList Income=new ArrayList();
        ArrayList Married=new ArrayList();
        ArrayList Children=new ArrayList();
        ArrayList Car=new ArrayList();
        ArrayList Save_act=new ArrayList();
        ArrayList Curent_act=new ArrayList();
        ArrayList Mortgage=new ArrayList();
        string duongdanTapTin = "";
        ArrayList YesOrNo = new ArrayList();
        //Chuc Nang Open
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogCSV = new OpenFileDialog();

            openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
            openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialogCSV.FilterIndex = 1;
            openFileDialogCSV.RestoreDirectory = true;

            if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
            {
                // gan ten day du
                this.fileCSV = openFileDialogCSV.FileName.ToString();

                // tao doi tuong System.IO.FileInfo de cat ten tap tin va duong dan.
                System.IO.FileInfo fi = new System.IO.FileInfo(this.fileCSV);
                // Lay ra duong dan
                this.dirCSV = fi.DirectoryName.ToString();
                // Lay ra ten va phan mo rong
                this.fileNevCSV = fi.Name.ToString();

                //nap du lieu len Datagridview
                this.dgvImportData.DataSource = LoadCSV();
                // database table name
                Classification();
            }

        }



        //Nap tap tin csv
        public DataTable LoadCSV()
        {
            DataTable dt = new DataTable();
            try
            {
                // Tao va mo ket noi ODBC
                string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                string sql_select;
                OdbcConnection conn;
                conn = new OdbcConnection(strConnString.Trim());
                conn.Open();

                //Viet cau lenh select
                sql_select = "select top " + numberOfRows + " * from [" + this.fileNevCSV.Trim() + "]";
                

                //Tao doi tuong dong bo du lieu
                OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);

                //va do du lieu vua nap tu tap tin csv vao dataset 'ds'
                obj_oledb_da.Fill(dt);

                //Dong ket noi
                conn.Close();
            }
            catch (Exception e) //Loi
            {
                MessageBox.Show(e.Message, "Lỗi! không mở được tập tin.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return dt;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn thoát không?", "Thoát", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        //phan lop
        public ArrayList Classification()
        {
            DataTable tCSVData = new DataTable();
            tCSVData = (DataTable)this.dgvImportData.DataSource;
            int iSoCot = tCSVData.Columns.Count;
            int iSoDong = tCSVData.Rows.Count;
            ArrayList arrKQ = new ArrayList();
            arrKQ.Add((iSoCot - 1).ToString());
           

            ArrayList[] ArrClass = new ArrayList[iSoCot];
            ArrayList ArrClassName = new ArrayList();
            for (int i = 0; i < iSoCot; i++)
            {
                ArrClass[i] = new ArrayList();
                ArrClassName.Add(tCSVData.Columns[i].ColumnName);
            }


            DataTable data = new DataTable();
            DataView dv = tCSVData.DefaultView;
            dv.Sort = tCSVData.Columns[iSoCot - 1].ColumnName + " ASC";
            data = dv.ToTable();
            int iIndexOfSo = ArrClassName.IndexOf(tCSVData.Columns[iSoCot - 1].ColumnName);
            ArrayList ArrCountSolution = new ArrayList();
            string LastValue = "";
            int LastIndex = 0;
            for (int i = 0; i < iSoDong; i++)
            {
                string value = data.Rows[i][iSoCot - 1].ToString();
                if (i == 0)
                {
                    ClsClass Class = new ClsClass();
                    Class.StrValue = value;
                    Class.ICount = 1;
                    ArrCountSolution.Add(Class);
                    LastValue = value;
                }else
                {
                    if (string.Compare(value, LastValue) == 0)
                    {
                        ClsClass Class = new ClsClass();
                        Class = (ClsClass)ArrCountSolution[LastIndex];
                        Class.ICount++;
                    }
                    else
                    {
                        ClsClass Class = new ClsClass();
                        Class.StrValue = value;
                        Class.ICount = 1;
                        ArrCountSolution.Add(Class);
                        LastValue = value;
                        LastIndex++;
                    }
                }
            }
            ArrClass[iIndexOfSo] = ArrCountSolution;

            arrKQ.Add(tCSVData.Columns[iSoCot - 1].ColumnName + " " + ArrCountSolution.Count);
            for (int i = 0; i < ArrCountSolution.Count; i++)
            {
                ClsClass Class = new ClsClass();
                Class = (ClsClass)ArrCountSolution[i];
                arrKQ.Add(Class.StrValue + " " + Class.ICount);
            }


            for (int j = 1; j < iSoCot - 1; j++)
            {
                dv = tCSVData.DefaultView;
                dv.Sort = tCSVData.Columns[j].ColumnName + " ASC";
                data = dv.ToTable();
                int iIndexOf = ArrClassName.IndexOf(tCSVData.Columns[j].ColumnName);
                ArrayList ArrCountValue = new ArrayList();
                LastValue = "";
                LastIndex = 0;
                for (int i = 0; i < iSoDong; i++)
                {
                    string value = data.Rows[i][j].ToString();
                    string CValue = data.Rows[i][iSoCot-1].ToString();
                    if (i == 0)
                    {
                        ClsContainClass CClass = new ClsContainClass();
                        ArrayList ArrCount = new ArrayList();
                        for (int k = 0; k < ArrCountSolution.Count; k++)
                        {
                            ClsClass Class = new ClsClass();
                            ClsClass ClassS = new ClsClass();
                            ClassS = (ClsClass)ArrCountSolution[k];
                            Class.StrValue = ClassS.StrValue;
                            if (string.Compare(CValue, Class.StrValue) == 0)
                            {
                                Class.ICount = 1;
                            }
                            else
                            {
                                Class.ICount = 0;
                            }
                            ArrCount.Add(Class);
                        }
                        CClass.ArrCount = ArrCount;
                        CClass.StrValue = value;

                        ArrCountValue.Add(CClass);
                        LastValue = value;
                    }
                    else
                    {
                        if (string.Compare(value, LastValue) == 0)
                        {
                            ClsContainClass CClass = new ClsContainClass();
                            CClass = (ClsContainClass)ArrCountValue[LastIndex];
                            for (int k = 0; k < CClass.ArrCount.Count; k++)
                            {
                                ClsClass ClassS = new ClsClass();
                                ClassS = (ClsClass)CClass.ArrCount[k];
                                if (string.Compare(ClassS.StrValue, CValue) == 0)
                                {
                                    ClassS.ICount++;
                                }
                            }
                        }
                        else
                        {
                            ClsContainClass CClass = new ClsContainClass();
                            ArrayList ArrCount = new ArrayList();
                            for (int k = 0; k < ArrCountSolution.Count; k++)
                            {
                                ClsClass Class = new ClsClass();
                                ClsClass ClassS = new ClsClass();
                                ClassS = (ClsClass)ArrCountSolution[k];
                                Class.StrValue = ClassS.StrValue;
                                if (string.Compare(CValue, Class.StrValue) == 0)
                                {
                                    Class.ICount = 1;
                                }
                                else
                                {
                                    Class.ICount = 0;
                                }
                                ArrCount.Add(Class);
                            }
                            CClass.ArrCount = ArrCount;
                            CClass.StrValue = value;

                            ArrCountValue.Add(CClass);
                            LastValue = value;
                            LastIndex++;
                        }
                    }
                }

                ArrClass[iIndexOf] = ArrCountValue;


                arrKQ.Add(tCSVData.Columns[j].ColumnName + " " + ArrCountValue.Count);
                for (int i = 0; i < ArrCountValue.Count; i++)
                {
                    ClsContainClass CClass = new ClsContainClass();
                    CClass = (ClsContainClass)ArrCountValue[i];
                    string test = CClass.StrValue;
                    
                    for (int k = 0; k < CClass.ArrCount.Count; k++)
                    {
                        ClsClass ClassS = new ClsClass();
                        ClassS = (ClsClass)CClass.ArrCount[k];

                        test += " " + ClassS.StrValue + " "+ClassS.ICount +" ";
                    }
                    arrKQ.Add(test);
                   
                }               
            }
            return arrKQ;
 

        }

        public void XuatTapTin(ArrayList Test)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.ShowDialog();
            duongdanTapTin = save.FileName + ".txt";
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@save.FileName+".txt"))
            {
                foreach (string line in Test)
                {

                    file.WriteLine(line);
   
                }
            }
        }

      
      ////  public void DocTapTin(){
      //      String DuongDan = @"D:\download\08\5555.txt";
      //      //string line;

      //      // Read the file and display it line by line.
      //      System.IO.StreamReader file = new System.IO.StreamReader(DuongDan);

            
      //      string input = file.ReadLine();
      //      int cout = int.Parse(input);

      //      ClsContainClass contain = new ClsContainClass();
      //      ArrayList arr = new ArrayList();
      //      ClsClass phanlop = new ClsClass();
      //      string[] chuoi;
      //      string bien1, bien_2;

      //      for (int i = 0; i < cout; i++)
      //      {
      //          input = file.ReadLine();
      //          chuoi = input.Split(' ');
      //          bien1 = chuoi[0].ToString();
      //          bien_2 = chuoi[1].ToString();
      //          int Pep = int.Parse(bien_2);

      //          phanlop.ICount = Pep;
      //          phanlop.StrValue = bien1;

      //          arr.Add(phanlop);

      //          for (int j = 0; j < Pep; j++)
      //          {
      //              arr.Add(phanlop);
      //          }

      //          file.Close();
      //          System.Console.WriteLine("There were {0} lines.", counter);
      //          // Suspend the screen.
      //          System.Console.ReadLine();
      //      }
      //  }

        private void exportClassifiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XuatTapTin(Classification());
        }



        private void frmMain_Load(object sender, EventArgs e)
        {

        }
        void KhoiTao()
        {
            FileInfo f1 = new FileInfo(@duongdanTapTin);
            if (f1.Exists == true)
            {
                StreamReader sr = File.OpenText(@duongdanTapTin);
                iSoCot = int.Parse(sr.ReadLine());
                for (int i = 0; i < iSoCot; i++)
                {
                    if (i == 0)
                    {
                        string input = sr.ReadLine();
                        string input2 = sr.ReadLine();
                        string input3 = sr.ReadLine();
                        string[] chuoi = input2.Split(' ');
                        C1 = int.Parse(chuoi[1]);
                        chuoi = input3.Split(' ');
                        C2 = int.Parse(chuoi[1]);
                        PC1 = (float)C1 / (C2 + C1);
                        PC2 = (float)C2 / (C1 + C2);
                    }
                    if (i == 1)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Age.Add(dt);
                        }
                    }
                    if (i == 2)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Sex.Add(dt);
                        }
                    }
                    if (i == 3)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Region1.Add(dt);
                        }
                    }
                    if (i == 4)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Income.Add(dt);
                        }
                    }
                    if (i == 5)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Married.Add(dt);
                        }
                    }
                    if (i == 6)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Children.Add(dt);
                        }
                    }
                    if (i == 7)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Car.Add(dt);
                        }
                    }
                    if (i == 8)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Save_act.Add(dt);
                        }
                    }
                    if (i == 9)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Curent_act.Add(dt);
                        }
                    }
                    if (i == 10)
                    {
                        string input = sr.ReadLine();
                        string[] chuoi = input.Split(' ');
                        int k = int.Parse(chuoi[1]);
                        for (int j = 0; j < k; j++)
                        {
                            ClsClassDT dt = new ClsClassDT();
                            string input3 = sr.ReadLine();
                            string[] chuoi1 = input3.Split(' ');
                            dt.iGIATRI = chuoi1[0];
                            dt.FPNo = float.Parse(chuoi1[2]) / C1;
                            dt.FPYes = float.Parse(chuoi1[5]) / C2;
                            Mortgage.Add(dt);
                        }
                    }
                }
                sr.Close();
            }
        }
        void RutLuat()
        {
            DataTable tCSVData = new DataTable();
            tCSVData = (DataTable)this.dgvImportData.DataSource;
            int inCot = tCSVData.Columns.Count;
            int inDong = tCSVData.Rows.Count;
            // string chuoi = tCSVData.Rows[0][0].ToString();
            for (int i = 0; i < inDong; i++)
            {

                float GiaTriYes = 1;
                float GiaTriNo = 1;
                for (int j = 1; j < inCot - 1; j++)
                {
                    if (j == 1)
                    {
                        foreach (object ob in Age)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 2)
                    {
                        foreach (object ob in Sex)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 3)
                    {
                        foreach (object ob in Region1)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 4)
                    {
                        foreach (object ob in Income)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 5)
                    {
                        foreach (object ob in Married)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 6)
                    {
                        foreach (object ob in Children)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 7)
                    {
                        foreach (object ob in Car)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 8)
                    {
                        foreach (object ob in Save_act)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 9)
                    {
                        foreach (object ob in Curent_act)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                    if (j == 10)
                    {
                        foreach (object ob in Mortgage)
                        {
                            ClsClassDT dt = (ClsClassDT)ob;
                            if (tCSVData.Rows[i][j].ToString() == dt.iGIATRI)
                            {
                                GiaTriNo = GiaTriNo * dt.FPNo;
                                GiaTriYes = GiaTriYes * dt.FPYes;
                            }
                        }
                    }
                }
                if (GiaTriYes > GiaTriNo)
                {
                    string kqqqq = "Yes";
                    YesOrNo.Add(kqqqq);
                }
                else
                {
                    string kqqqq1 = "No";
                    YesOrNo.Add(kqqqq1);
                }
            }
            
        }
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void inputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogCSV = new OpenFileDialog();

            openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
            openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialogCSV.FilterIndex = 1;
            openFileDialogCSV.RestoreDirectory = true;

            if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
            {
                // gan ten day du
                this.fileCSV = openFileDialogCSV.FileName.ToString();

                // tao doi tuong System.IO.FileInfo de cat ten tap tin va duong dan.
                System.IO.FileInfo fi = new System.IO.FileInfo(this.fileCSV);
                // Lay ra duong dan
                this.dirCSV = fi.DirectoryName.ToString();
                // Lay ra ten va phan mo rong
                this.fileNevCSV = fi.Name.ToString();

                //nap du lieu len Datagridview
                this.dgvImportData.DataSource = LoadCSV();
                // database table name
                KhoiTao();

            }
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RutLuat();
            XuatTapTin(YesOrNo);
        }


 

        //Kha Nguyễn - 5/18/2013 - end
    }
}
